package com.atguigu.blog.service.impl;

import com.atguigu.blog.entity.Category;
import com.atguigu.blog.mapper.CategoryMapper;
import com.atguigu.blog.service.CategoryService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 1.2 文章类型展现
     * @author Petrichor
     * @date 2023/10/23 16:10
     **/
    @Override
    public List<Category> getCategory() {
        return categoryMapper.getCategory();
    }
    /**
     * 4.1 分类列表
     * @author Petrichor
     * @date 2023/10/24 18:04
     **/
    @Override
    public PageInfo findCategoryByPage(int page, int limit) {
        PageHelper.startPage(page, limit);
        List<Category> categoryList = categoryMapper.getCategory();
        PageInfo pageInfo = new PageInfo(categoryList,5);
        return pageInfo;
    }
    /**
     * 4.2 根据名称查询分类信息
     * @author Petrichor
     * @date 2023/10/24 18:36
     **/
    @Override
    public List<Category> findCategoryByName(String cname) {
        return categoryMapper.findCategoryByName(cname);
    }
    /**
     * 4.3 文章分类新增
     * @author Petrichor
     * @date 2023/10/24 19:58
     **/
    @Override
    public void saveCategory(Category category) {
        category.setCreateTime(new Date());
        category.setUpdateTime(category.getCreateTime());
        categoryMapper.saveCategory(category);
    }
    /**
     * 4.4 文章分类修改
     * @author Petrichor
     * @date 2023/10/24 19:58
     **/
    @Override
    public void updateCategory(Category category) {
        category.setUpdateTime(new Date());
        categoryMapper.updateCategory(category);
    }
    /**
     * 4.5 文章分类删除
     * @author Petrichor
     * @date 2023/10/24 20:22
     **/
    @Override
    public void deleteCategoryById(Long id) {
        categoryMapper.deleteCategoryById(id);
    }
}
